package com.example.demo1;

import com.example.demo1.service.IUserService;
import com.example.demo1.service.impl.UserServiceImpl;
import com.example.demo1.util.HttpClientUtils;
import com.example.demo1.util.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@SpringBootApplication
@RestController
@Slf4j
@MapperScan("com.example.demo1.mapper")
public class Demo1Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext ac = SpringApplication.run(Demo1Application.class, args);
        OkHttpClient okHttpClient = ac.getBean(OkHttpClient.class);
        System.out.println(okHttpClient);
//        ac.getBeanNamesForType(IUserService.class);
        System.out.println(ac.getBean(IUserService.class));
        System.out.println(ac.getBean(UserServiceImpl.class));
    }
    
    @Autowired
    private HttpClientUtils httpClientUtils;

    @PostMapping("ca")
    @ResponseBody
    public String post(@RequestBody Map<String, Object> map){
        log.info("{}", map);
        return "";
    }


    @GetMapping("test")
    public String get(){
/*        long start = System.currentTimeMillis();
        for(int i=0;i<10000;i++) {
            httpClientUtils.doGet("http://www.baidu.com");
        }
        long end = System.currentTimeMillis();
        log.info("httpClientUtils 总耗时：{}毫秒 - {}秒", end-start ,(end-start)/1000);
        long start1 = System.currentTimeMillis();
        for(int i=0;i<1;i++) {
            OkHttpUtil.get("http://www.baidu.com", null);
        }
        long end1 = System.currentTimeMillis();
        log.info("OkHttpUtil 总耗时：{}毫秒 - {}秒", end1-start1 ,(end1-start1)/1000);*/
//        log.info(s);
        long start = System.currentTimeMillis();
        String url = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl";
        String soapXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><TranslatorString xmlns=\"http://WebXml.com.cn/\"><wordKey>test</wordKey></TranslatorString></soap12:Body></soap12:Envelope>";
        String result = httpClientUtils.doPostSoapXml(url, soapXml);
        log.info("webservice =====> result : {}", result);
        long end = System.currentTimeMillis();
        log.info("OkHttpUtil 总耗时：{}毫秒 - {}秒", end-start ,(end-start)/1000);
        for(int i=0;i<10;i++) {
            start = System.currentTimeMillis();
            url = "http://10.242.0.51/data-transfer/ws/RoadTransService?wsdl";
            soapXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><Command xmlns=\"http://roadtrans.service.cttic.cn/\"><transaction xmlns=\"\"><header><serialnum>66000020210720141007752123135785</serialnum><sourceservice>660000</sourceservice><targetservice>000000</targetservice><systemid>000000</systemid><serviceid>QE0101</serviceid><date>20210720</date><time>141007</time><isreq>0</isreq><signature><md5>C6EF49378CA521B0EB08D00D2024A0DC</md5><detail>Qec1HNHF6n0K/RYBmmJTUxI+gQ/bzjQvfLaTly9oUE34iUNMCyj3O0fryIzIFKAn0lxrh2QCOGyYHJSk2MhhsQ==</detail></signature><response></response></header><body>&lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;reqInfo&gt;&lt;idCard&gt;130721198306111113&lt;/idCard&gt;&lt;idType&gt;1&lt;/idType&gt;&lt;provinceCode&gt;&lt;/provinceCode&gt;&lt;pageInfo&gt;&lt;page&gt;1&lt;/page&gt;&lt;/pageInfo&gt;&lt;/reqInfo&gt;</body></transaction></Command></soap:Body></soap:Envelope>";
            result = httpClientUtils.doPostSoapXml(url, soapXml);
            log.info("webservice =====> result : {}", result);
            end = System.currentTimeMillis();
            log.info("OkHttpUtil 总耗时：{}毫秒 - {}秒", end - start, (end - start) / 1000);
        }
        return "ok";
    }

}
